package com.tyz.hellocst;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.tyz.hellocst.adapter.MyFragmentPagerAdapter;
import com.tyz.hellocst.database.DBOperate;
import com.tyz.hellocst.entity.Goods;
import com.tyz.hellocst.entity.Person;
import com.tyz.hellocst.fragment.AddGoodsFragment;
import com.tyz.hellocst.fragment.HomePageFragment;
import com.tyz.hellocst.fragment.PersonalFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ViewPager viewPager;
    private TabLayout tabLayout;
    private List<Fragment> fragments;
    private FragmentManager fm;
    private FragmentTransaction ft;
    HomePageFragment hpf;
    AddGoodsFragment adf;
    PersonalFragment pf;
    MyFragmentPagerAdapter mfpa;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tabLayout = (TabLayout) findViewById(R.id.tabs2);
        viewPager = (ViewPager) findViewById(R.id.viewpager);

        initData();
        //ceshitianjia();

    }

    private void initData() {

        List<String> titles = new ArrayList<>();
        titles.add(getString(R.string.title_activity_homePage));
        titles.add(getString(R.string.title_activity_addGoods));
        titles.add(getString(R.string.title_activity_personal));
        fragments = new ArrayList<>();
        hpf = new HomePageFragment();
        adf = new AddGoodsFragment();
        pf = new PersonalFragment();
        fragments.add(hpf);
        fragments.add(adf);
        fragments.add(pf);
        fm = getSupportFragmentManager();
        mfpa=new MyFragmentPagerAdapter(fm,FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
        mfpa.setFragmentList(fragments,titles);
        ft = fm.beginTransaction();
        ft.commit();
        viewPager.setAdapter(mfpa);
        tabLayout.addTab(tabLayout.newTab().setText(titles.get(0)),true);
        tabLayout.addTab(tabLayout.newTab().setText(titles.get(1)),false);
        tabLayout.addTab(tabLayout.newTab().setText(titles.get(2)),false);
        tabLayout.setupWithViewPager(viewPager);


    }

    private void ceshitianjia() {
        DBOperate dbOperate = new DBOperate(this);
        dbOperate.openDB();
        Person person = new Person("root", "password","17726781042", "慕苏杭", "男");
        dbOperate.addPerson(person);
        dbOperate.addPerson(new Person("admin", "admin123", "18154905373","唐于朱", "女"));
        //
        Bitmap bitmap[] = new Bitmap[6];
        int id[] = new int[]{R.mipmap.good1, R.mipmap.good2,
                R.mipmap.good3, R.mipmap.good4, R.mipmap.good5, R.mipmap.good6};
        Goods goods[] = new Goods[6];
        String goodsId[] = new String[]{"goodsId1", "goodsId2", "goodsId3", "goodsId4", "goodsId5","goodsId6"};
        String goodsName[] = new String[]{"卡西欧 经典系列 男士手表", "万代 拼插 模型", "DURGOD 三模 机械键盘", "微星 RGB光效 鼠标", "耐克 缓震 跑步鞋","李宁 吸湿PU 篮球"};
        String goodsText[] = new String[]{"这款卡西欧手表，采用军绿色系外观设计，散发着沉稳而又时尚的光芒。表盘上带有醒目的数字显示，可直观的看清时间日期。",
                "这款高达模型采用了优质的塑料材质，具有很好的耐摔性。同时，它还具有很好的观赏性，让孩子们可以轻松的拼装出自己喜欢的造型。",
                "哑光喷漆机身，全按键无冲突，3设备切换。选用哑光喷漆工艺制造机身，质感更好，全按键无冲突技术，快速敲击快速响应。",
                "RGB光效，钢琴烤漆质感涂层。支持RGB光效，拥有七种灯效，可以自由选择喜欢的灯效，更支持软件DIY灯效，带来更优质的使用体验。",
                "耐克的这款跑步鞋，采用了缓震的性能，能够有效的缓解脚部的压力，让你的每一步都更加的轻松舒适。",
                "PU材质，防滑耐磨。这款篮球采用PU材质，具有良好的防滑性，搭载防滑耐磨的中胎，让你轻松应对各种场地，运动更加的安全。"};
        String goodsTime[] = new String[]{"2021-07-19 15:32:59", "2021-10-20 13:35:49",
                "2021-11-04 20:32:02", "2021-12-08 07:54:36", "2021-12-20 09:04:16","2021-12-27 18:44:38"};
        double goodsPrice[] = new double[]{199.0, 223, 108, 41, 135,88};
        String goodsOwner[] = new String[]{"admin", "root", "admin", "admin", "root","root"};
        String goodsOwnerContact[] = new String[]{"13939404058", "2987345656",
                "13461415066", "98765432101", "98765432101","13939404058"};
        for (int i = 0; i < 6; i++) {
            bitmap[i] = BitmapFactory.decodeResource(getResources(), id[i]);
            goods[i] = new Goods(goodsId[i], goodsName[i], goodsText[i],
                    bitmap[i], goodsTime[i], goodsPrice[i],
                    goodsOwner[i], goodsOwnerContact[i]);
            dbOperate.addGoods(goods[i]);
        }
        dbOperate.closeDb();
    }



}
